<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>声明</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/declarations","wgTitle":"cpp/language/declarations","wgCurRevisionId":62838,"wgArticleId":9179,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/declarations","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_declarations skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">声明</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><strong class="selflink">声明</strong><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">声明语法</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 简单声明</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 连接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 属性声明</strong> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 空声明</strong> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p><i>声明</i>将名字引入（或再引入）到 C++ 程序中。每种实体的声明方式都不同。<a href="definition.html" title="cpp/language/definition">定义</a>是足以使该名字所标识的实体被使用的声明。
</p><p>声明是下列之一：
</p>
<ul><li> <a href="function.html#.E5.87.BD.E6.95.B0.E5.AE.9A.E4.B9.89" title="cpp/language/function">函数定义</a>
</li><li> <a href="templates.html" title="cpp/language/templates">模板声明</a>（包括<a href="partial_specialization.html" title="cpp/language/partial specialization">部分模板特化</a>）
</li><li> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template">显式模板实例化</a>
</li><li> <a href="template_specialization.html" title="cpp/language/template specialization">显式模板特化</a>
</li><li> <a href="namespace.html" title="cpp/language/namespace">命名空间定义</a>
</li><li> <a href="language_linkage.html" title="cpp/language/language linkage">连接说明</a>
</li><li> 属性声明（<span class="t-spar"><a href="attributes.html" title="cpp/language/attributes">attr</a></span> <code><b>;</b></code>）<span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li> 空声明（<code><b>;</b></code>）<span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li> 无 <span class="t-spar">声明说明符序列</span> 的函数声明：
</li></ul>
<table class="t-sdsc-begin">
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">attr</span> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td>
<td> -
</td>
<td> 任意数量<a href="attributes.html" title="cpp/language/attributes">属性</a>的序列
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">声明符</span>
</td>
<td> -
</td>
<td> 函数声明符。
</td></tr></table>
<dl><dd> 此声明必须声明构造函数、析构函数或用户定义的类型<a href="cast_operator.html" title="cpp/language/cast operator">转换函数</a>。它只能用作<a href="templates.html" title="cpp/language/templates">模板声明</a>、<a href="template_specialization.html" title="cpp/language/template specialization">显式特化</a>或显式实例化的一部分。
</dd></dl>
<ul><li> <span class="t-spar">块声明</span>（能出现于<a href="statements.html#.E5.A4.8D.E5.90.88.E8.AF.AD.E5.8F.A5" title="cpp/language/statements">块</a>中的声明），它可以是下列之一：
</li></ul>
<dl><dd><ul><li><a href="asm.html" title="cpp/language/asm">汇编声明</a>
</li><li><a href="type_alias.html" title="cpp/language/type alias">类型别名声明</a> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li><a href="namespace_alias.html" title="cpp/language/namespace alias">命名空间别名定义</a>
</li><li><a href="using_declaration.html" title="cpp/language/using declaration">using 声明</a>
</li><li><a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace">using 指令</a>
</li><li><a href="static_assert.html" title="cpp/language/static assert">static_assert</a> 声明 <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li><a href="enum.html" title="cpp/language/enum">不可见 enum 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li>简单声明
</li></ul>
</dd></dl>
<h3><span class="mw-headline" id=".E7.AE.80.E5.8D.95.E5.A3.B0.E6.98.8E">简单声明</span></h3>
<p>简单声明是引入、创建并可选地初始化一个或数个标识符（典型地为变量）的语句。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">声明说明符序列</span> <span class="t-spar">初始化声明符列表</span><span class="t-mark">(可选)</span> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">attr</span> <span class="t-spar">声明说明符序列</span> <span class="t-spar">初始化声明符列表</span><code><b>;</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">attr</span> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td>
<td> -
</td>
<td> 任何数量<a href="attributes.html" title="cpp/language/attributes">属性</a>的序列
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">声明说明符序列（decl-specifier-seq）</span>
</td>
<td> -
</td>
<td> <i>说明符（specifier）</i>的序列（见下文）。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">初始化声明符列表（init-declarator-list）</span>
</td>
<td> -
</td>
<td> 带可选的<i>初始化器（initializer）</i>的<i>声明符（declarator）</i>的逗号分隔列表。 <span class="t-spar">初始化声明符列表</span> 在声明具名的 class/struct/union 或具名枚举时是可选的
</td></tr></table>
<p><span class="t-rev-inl t-since-cxx17"><span><a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a>亦为简单声明。</span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>
</p>
<h3><span class="mw-headline" id=".E8.AF.B4.E6.98.8E.E7.AC.A6">说明符</span></h3>
<p><b>声明说明符</b>（<span class="t-spar">声明说明符序列</span>）是下列以空白分隔的说明符的序列，顺序任意：
</p>
<ul><li> <a href="typedef.html" title="cpp/language/typedef"><code>typedef</code></a> 说明符。若存在，则整个声明是 <a href="typedef.html" title="cpp/language/typedef">typedef 声明</a>，且每个声明符引入一个新的类型名，而非对象或函数。
</li><li> 函数说明符（<a href="inline.html" title="cpp/language/inline"><code>inline</code></a>、<a href="virtual.html" title="cpp/language/virtual"><code>virtual</code></a>、<a href="explicit.html" title="cpp/language/explicit"><code>explicit</code></a>），仅在<a href="function.html" title="cpp/language/function">函数声明</a>中允许使用。
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<ul><li> <a href="inline.html" title="cpp/language/inline"><code>inline</code></a> 说明符亦允许在变量声明上使用。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<ul><li> <a href="friend.html" title="cpp/language/friend"><code>friend</code></a> 说明符允许在类和函数声明中使用。
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<ul><li> <a href="constexpr.html" title="cpp/language/constexpr"><code>constexpr</code></a> 说明符，只允许在变量定义，函数及函数模板声明，以及具有字面类型的静态数据成员的声明中使用。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<ul><li> <a href="consteval.html" title="cpp/language/consteval"><code>consteval</code></a> 说明符，只允许在函数和函数模板声明中使用。
</li><li> <a href="constinit.html" title="cpp/language/constinit"><code>constinit</code></a> 说明符，只允许在拥有静态或线程存储期的变量声明中使用。一个 <span class="t-spar">声明说明符序列</span> 中至多允许出现 <code>constexpr</code> 、 <code>consteval</code> 和 <code>constinit</code> 说明符中的一者。
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<ul><li> <a href="storage_duration.html" title="cpp/language/storage duration">存储类说明符</a>（<a href="../keyword/register.html" title="cpp/keyword/register"><tt>register</tt></a>、<a href="../keywords/static.html" title="cpp/keyword/static"><tt>static</tt></a><span class="t-rev-inl t-since-cxx11"><span>、<a href="../keyword/thread_local.html" title="cpp/keyword/thread local"><tt>thread_local</tt></a></span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>、<a href="../keyword/extern.html" title="cpp/keyword/extern"><tt>extern</tt></a>、<a href="../keyword/mutable.html" title="cpp/keyword/mutable"><tt>mutable</tt></a>）。仅允许使用一个存储类说明符，但 <code>thread_local</code> 可以与 <code>extern</code> 或 <code>static</code> 一同出现。
</li><li> <b>类型说明符</b>（<span class="t-spar">类型说明符序列</span>），指名一个类型的说明符的序列。声明所引入的每个实体均为此类型，可选地为声明符所修饰（见下文）。这个说明符序列也被<a href="incomplete_type.html#.E7.B1.BB.E5.9E.8B.E7.9A.84.E5.91.BD.E5.90.8D" title="cpp/language/type">类型标识（type-id）</a>所用。唯有下列说明符是 <span class="t-spar">类型说明符序列</span> 的一部分，顺序任意：
</li></ul>
<dl><dd><ul><li> <a href="class.html" title="cpp/language/class">类说明符</a>
</li><li> <a href="enum.html" title="cpp/language/enum">枚举说明符</a>
</li><li> 简单类型说明符
</li></ul>
<dl><dd><ul><li><a href="../keyword/char.html" title="cpp/keyword/char"><tt>char</tt></a><span class="t-rev-inl t-since-cxx20"><span>、<a href="../keyword/char8_t.html" title="cpp/keyword/char8 t"><tt>char8_t</tt></a></span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span><span class="t-rev-inl t-since-cxx11"><span>、<a href="../keyword/char16_t.html" title="cpp/keyword/char16 t"><tt>char16_t</tt></a>、<a href="../keyword/char32_t.html" title="cpp/keyword/char32 t"><tt>char32_t</tt></a></span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>、<a href="../keyword/wchar_t.html" title="cpp/keyword/wchar t"><tt>wchar_t</tt></a>、<a href="../keyword/bool.html" title="cpp/keyword/bool"><tt>bool</tt></a>、<a href="../keyword/short.html" title="cpp/keyword/short"><tt>short</tt></a>、<a href="../keyword/int.html" title="cpp/keyword/int"><tt>int</tt></a>、<a href="../keyword/long.html" title="cpp/keyword/long"><tt>long</tt></a>、<a href="../keyword/signed.html" title="cpp/keyword/signed"><tt>signed</tt></a>、<a href="../keyword/unsigned.html" title="cpp/keyword/unsigned"><tt>unsigned</tt></a>、<a href="../keyword/float.html" title="cpp/keyword/float"><tt>float</tt></a>、<a href="../keyword/double.html" title="cpp/keyword/double"><tt>double</tt></a>、<a href="../keyword/void.html" title="cpp/keyword/void"><tt>void</tt></a>
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<dl><dd><dl><dd><ul><li><a href="auto.html" title="cpp/language/auto">auto</a>
</li><li><a href="decltype.html" title="cpp/language/decltype">decltype 说明符</a>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<dl><dd><dl><dd><ul><li> 先前声明过的类名（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）
</li><li> 先前声明过的枚举名（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）
</li><li> 先前声明过的 <a href="typedef.html" title="cpp/language/typedef">typedef 名</a><span class="t-rev-inl t-since-cxx11"><span>或<a href="type_alias.html" title="cpp/language/type alias">类型别名</a></span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）
</li><li> 带模板实参的模板名（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>，可选地用 <a href="dependent_name.html" title="cpp/language/dependent name">template 消歧义符</a>）
</li></ul>
</dd></dl>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<dl><dd><dl><dd><ul><li>无模板实参的模板名（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）：参阅<a href="class_template_argument_deduction.html" title="cpp/language/class template argument deduction">类模板实参推导</a>
</li></ul>
</dd></dl>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<dl><dd><ul><li> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a>
</li></ul>
<dl><dd><ul><li> 关键词 <a href="../keyword/class.html" title="cpp/keyword/class"><tt>class</tt></a>、<a href="../keyword/struct.html" title="cpp/keyword/struct"><tt>struct</tt></a> 或 <a href="../keyword/union.html" title="cpp/keyword/union"><tt>union</tt></a>，后随先前已定义为 <code>class</code>、<code>struct</code> 或 <code>union</code> 名的标识符（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）。
</li><li> 关键词 <a href="../keyword/class.html" title="cpp/keyword/class"><tt>class</tt></a>、<a href="../keyword/struct.html" title="cpp/keyword/struct"><tt>struct</tt></a> 或 <a href="../keyword/union.html" title="cpp/keyword/union"><tt>union</tt></a>，后随带模板实参的、先前已定义为类模板名字的模板名（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>，可选地用 <a href="dependent_name.html" title="cpp/language/dependent name">template 消歧义符</a>）。
</li><li> 关键词 <a href="../keyword/enum.html" title="cpp/keyword/enum"><tt>enum</tt></a> 后随先前已定义为枚举名的标识符（可选地<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定</a>）。
</li></ul>
</dd></dl>
<ul><li> <a href="dependent_name.html" title="cpp/language/dependent name">typename 说明符</a>
</li><li> <a href="cv.html" title="cpp/language/cv">cv 限定符</a>
</li></ul>
</dd><dd>每个 <span class="t-spar">声明说明符序列</span> 中只允许一个类型说明符，但有以下例外：
</dd><dd>- <code>const</code> 能与自身外的任何类型说明符组合。
</dd><dd>- <code>volatile</code> 能与自身外的任何类型说明符组合。
</dd><dd>- <code>signed</code> 或 <code>unsigned</code> 能与 <code>char</code>、<code>long</code>、<code>short</code> 或 <code>int</code> 组合。
</dd><dd>- <code>short</code> 或 <code>long</code> 能与 <code>int</code> 组合。
</dd><dd>- <code>long</code> 能与 <code>double</code> 组合。
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<dl><dd>- <code>long</code> 能与 <code>long</code> 组合。
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<p><a href="attributes.html" title="cpp/language/attributes">属性</a>可出现于 <span class="t-spar">声明说明符序列</span> 中，该情况下它们应用于其之前的说明符所确定的类型。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p><span class="t-spar">声明说明符序列</span> 中仅有的允许两次出现的说明符是 <code>long</code>（可出现两次）。所有其他重复，例如 <code>const static const</code> 或 <code>virtual inline virtual</code> 均为错误。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E5.A3.B0.E6.98.8E.E7.AC.A6">声明符</span></h3>
<p><span class="t-spar">初始化声明符列表（init-declarator-list）</span>是一或多个 <span class="t-spar">初始化声明符（init-declarator）</span> 的逗号分隔列表，它拥有下列语法：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">声明符</span> <span class="t-spar">初始化器</span><span class="t-mark">(可选)</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">声明符</span> <span class="t-spar">requires-子句</span>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">声明符（declarator）</span>
</td>
<td> -
</td>
<td> 声明符
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">初始化器（initializer）</span>
</td>
<td> -
</td>
<td> 可选的初始化器（除非在必要的场合，例如初始化引用或 const 对象时）。细节见<a href="initialization.html" title="cpp/language/initialization">初始化</a>。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">requires-子句</span><span class="t-mark-rev t-since-cxx20">(C++20)</span>
</td>
<td> -
</td>
<td> <a href="constraints.html#requires_.E5.AD.90.E5.8F.A5" title="cpp/language/constraints">requires 子句</a>，向<a href="function.html" title="cpp/language/function">函数声明</a>添加<a href="constraints.html" title="cpp/language/constraints">制约</a>
</td></tr></table>
<p>初始化声明符序列 <span class="t-c"><span class="mw-geshi cpp source-cpp">S D1, D2, D3<span class="sy4">;</span></span></span> 中的每个 <span class="t-spar">初始化声明符</span>，均按照如同它是拥有相同说明符的孤立声明来处理：<span class="t-c"><span class="mw-geshi cpp source-cpp">S D1<span class="sy4">;</span> S D2<span class="sy4">;</span> S D3<span class="sy4">;</span></span></span>。
</p><p>每个声明符恰好引入一个对象、引用、函数或（对于 typedef 声明）类型别名，其类型由 <span class="t-spar">声明说明符序列</span> 提供，并可选地被声明符中的运算符，如 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy3">&amp;</span></span></span>（～的引用）或 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">[</span><span class="br0">]</span></span></span>（～的数组）或 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">(</span><span class="br0">)</span></span></span>（返回～的函数）所修饰。可以递归应用这些声明符，如下所示。
</p><p><span class="t-spar">声明符</span> 是下列之一：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">无限定标识</span> <span class="t-spar">attr</span><span class="t-mark">(可选)</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">有限定标识</span> <span class="t-spar">attr</span><span class="t-mark">(可选)</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>...</b></code> <span class="t-spar">标识符</span> <span class="t-spar">attr</span><span class="t-mark">(可选)</span>
</td>
<td> (3)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>*</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">cv</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">嵌套名说明符</span> <code><b>*</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">cv</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&amp;</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (6)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>&amp;&amp;</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">声明符</span>
</td>
<td> (7)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">noptr-声明符</span> <code><b>[</b></code> <span class="t-spar">constexpr</span><span class="t-mark">(可选)</span> <code><b>]</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span>
</td>
<td> (8)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">noptr-声明符</span> <code><b>(</b></code> <span class="t-spar">形参列表</span> <code><b>)</b></code> <span class="t-spar">cv</span><span class="t-mark">(可选)</span> <span class="t-spar">ref</span><span class="t-mark">(可选)</span> <span class="t-spar">异常说明</span><span class="t-mark">(可选)</span> <span class="t-spar">attr</span><span class="t-mark">(可选)</span>
</td>
<td> (9)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 所声明的<a href="name.html#.E5.90.8D.E5.AD.97" title="cpp/language/identifiers">名称</a>。</div>
<div class="t-li1"><span class="t-li">2)</span> 使用<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">有限定的标识符</a>（<span class="t-spar">有限定标识</span>）定义或重声明先前声明的<a href="namespace.html#.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4" title="cpp/language/namespace">命名空间成员</a>或<a href="classes.html" title="cpp/language/classes">类成员</a>的声明符。</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="parameter_pack.html" title="cpp/language/parameter pack">形参包</a>，仅出现于<a href="function.html#.E5.BD.A2.E5.8F.82.E5.88.97.E8.A1.A8" title="cpp/language/function">形参声明</a>中。</div>
<div class="t-li1"><span class="t-li">4)</span> <a href="pointer.html" title="cpp/language/pointer">指针声明符</a>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S <span class="sy2">*</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为指向 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的指针。</div>
<div class="t-li1"><span class="t-li">5)</span> <a href="pointer.html" title="cpp/language/pointer">成员指针声明</a>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S C<span class="sy4">::</span><span class="sy2">*</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为指向 <code>C</code> 中的类型为 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的成员的指针。<span class="t-spar">嵌套名说明符</span> 为<a href="name.html#.E6.9C.89.E9.99.90.E5.AE.9A.E7.9A.84.E6.A0.87.E8.AF.86.E7.AC.A6" title="cpp/language/identifiers">名字和作用域解析运算符 <code><b>::</b></code> 的序列</a></div>
<div class="t-li1"><span class="t-li">6)</span> <a href="reference.html" title="cpp/language/reference">左值引用声明符</a>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S <span class="sy3">&amp;</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为到 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的左值引用。</div>
<div class="t-li1"><span class="t-li">7)</span> <a href="reference.html" title="cpp/language/reference">右值引用声明符</a>：声明 <span class="t-c"><span class="mw-geshi cpp source-cpp">S <span class="sy3">&amp;&amp;</span> D<span class="sy4">;</span></span></span> 将 <code>D</code> 声明为到 <span class="t-spar">声明说明符序列</span> 所确定的类型 <code>S</code> 的右值引用。</div>
<div class="t-li1"><span class="t-li">8)</span> <a href="array.html" title="cpp/language/array">数组声明符</a>。<span class="t-spar">noptr-声明符</span> 为任何合法声明符，但若它以 *、&amp; 或 &amp;&amp; 起始，则必须用括号环绕它。</div>
<div class="t-li1"><span class="t-li">9)</span> <a href="function.html" title="cpp/language/function">函数声明符</a>。<span class="t-spar">noptr-声明符</span> 为任何合法声明符，但若它以 *、&amp; 或 &amp;&amp; 起始，则必须用括号环绕它。注意最外层函数声明符可以可选的尾随返回类型结尾。</div>
<p>所有情况下，<span class="t-spar">attr</span> 均为可选的<a href="attributes.html" title="cpp/language/attributes">属性</a>的序列。当其紧跟标识符之后出现时，它应用于所声明的对象。
</p><p><span class="t-spar">cv</span> 是 <a href="cv.html" title="cpp/language/cv">const 与 volatile</a> 限定符的序列，其中任一限定符在序列中至多出现一次。
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：解释名字隐藏规则；变量/函数声明如何隐藏同名的类（而非 typedef） </td></tr></table>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p><span class="t-spar">块声明</span>出现<a href="statements.html#.E5.A4.8D.E5.90.88.E8.AF.AD.E5.8F.A5" title="cpp/language/statements">在代码块内</a>，而当声明中引入的标识符之前已在某个外层代码块中声明时，该代码块的剩余部分中<a href="scope.html" title="cpp/language/scope">外层声明被隐藏</a>。
</p><p>若声明引入具有自动存储期的变量，则它在执行其声明语句时初始化。退出代码块时，所有声明于该块中的自动变量被以其初始化顺序的相反顺序销毁（无关乎如何退出代码块：通过<a href="exceptions.html" title="cpp/language/exceptions">异常</a>、<a href="goto.html" title="cpp/language/goto">goto</a> 或抵达其结尾）。
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> C <span class="br0">{</span>
    <a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a> member<span class="sy4">;</span> <span class="co1">// 声明说明符序列 为 "std::string"</span>
                        <span class="co1">// 声明符 为 "member"</span>
<span class="br0">}</span> obj, <span class="sy2">*</span>pObj<span class="br0">(</span><span class="sy3">&amp;</span>obj<span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// 声明说明符序列 为 "class C { std::string member; }"</span>
<span class="co1">// 声明符 "obj" 定义 C 类型的对象</span>
<span class="co1">// 声明符 "*pObj(&amp;obj)" 声明并初始化指向 C 的指针</span>
 
<span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span>, <span class="sy2">*</span>p <span class="sy1">=</span> nullptr, f<span class="br0">(</span><span class="br0">)</span>, <span class="br0">(</span><span class="sy2">*</span>pf<span class="br0">)</span><span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
<span class="co1">// 声明说明符序列 为 int</span>
<span class="co1">// 声明符 a=1 定义并初始化 int 类型的变量</span>
<span class="co1">// 声明符 *p=NULL 定义并初始化 int* 类型的变量</span>
<span class="co1">// 声明符 (f)() 声明（但不定义）不接收参数并返回 int 的函数</span>
<span class="co1">// 声明符 (*pf)(double) 定义指向接收 double 并返回 int 的函数的指针</span>
 
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span><span class="br0">(</span><span class="sy2">*</span>foo<span class="br0">)</span><span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> nullptr<span class="sy4">;</span>
<span class="co1">// decl-specifier-seq 为 int</span>
<span class="co1">// 1. 声明符 "(*(*foo)(double))[3]" 是数组声明符；</span>
<span class="co1">//    所声明类型是“int 的 3 元素数组 /嵌套声明符/ ”</span>
<span class="co1">// 2. 嵌套声明符是 "(*(*foo)(double))"，它是指针声明符</span>
<span class="co1">//    所声明类型是“int 的 3 元素数组 的指针 /嵌套声明符/”</span>
<span class="co1">// 3. 嵌套声明符是 "(*foo)(double)"，它是函数声明符</span>
<span class="co1">//    所声明类型是“以 int 的 3 元素数组 的指针 为返回值的接受一个 double 参数的函数 /嵌套声明符/”</span>
<span class="co1">// 4. 嵌套声明符是 "(*foo)"，它是（带括号，如函数声明符语法所要求的）指针声明符。</span>
<span class="co1">//    所声明类型是“以 int 的 3 元素数组 的指针 为返回值的接受一个 double 参数的函数 的指针 /嵌套声明符/”</span>
<span class="co1">// 5. 嵌套声明符是 "foo"，它是标识符。</span>
<span class="co1">// 该声明声明了对象 foo，其类型为“以 int 的 3 元素数组 的指针 为返回值的接受一个 double 参数的函数 的指针”</span>
<span class="co1">// 初始化器 "= nullptr" 提供此指针的初值。</span></pre></div></div>
<p><br> 
</p>
</div>
<tr><td></td></tr>
</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 6572/1000000
Preprocessor generated node count: 14504/1000000
Post‐expand include size: 103233/2097152 bytes
Template argument size: 32166/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:9179-0!*!0!!zh!*!zh!* and timestamp 20191018165204 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/declarations&amp;oldid=62838">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/declarations&amp;oldid=62838</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/declarations">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2019年10月18日 (星期五) 09:45。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过13,894次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.071 secs. -->
	</body>
<!-- Cached 20191018212229 -->
</html>